
// return: {code}    // 1更新 2插入

var path = require('path')
var routerApi = require('../../utils/routeString.js')(__filename, __dirname);
var utils = require('../../utils/index.js')
var validate = utils.validate;
var ip = utils.ip;
var SqlHelper = utils.SqlHelper;

server.post({ path: routerApi, name: routerApi }, function (req, res, next) {
    var body = req.body || req.query;
    var cmd = parseInt(body.cmd);
    var sqlHelper = new SqlHelper('checkin_yuexiu_0929');

    if (cmd === 0) { // 拉取数据
        sqlHelper.select({ id: body.userid }, (results) => {
            return res.send({ results: results });
        })
    } else if (cmd === 1) { // 插入数据
        
        if (!validate.isName(body.name) || !validate.isPhone(body.phone)) {
            return res.send({ code: 0, msg: '请填写正确的电话和姓名' });
        }
        
        if (!body.userid) {
            return res.send({ code: 0, msg: '无效请求' });
        }

        var key = {
            id: body.userid
        }

        var data = {
            id: body.userid,
            name: body.name,
            phone: body.phone,
            number: body.number
        }

        var sqlHelper = new SqlHelper('checkin_yuexiu_0929');
        sqlHelper.pushData(key, data, (state, results) => {
            return res.send({ code: state });
        })

    } else {
        return res.send({ code: 0, msg: "cmd参数错误" });
    }





})
